import { FILE_ENUM } from '@/global/enum/common';
import { getFileExtension } from './file';
import {
  DOC_EXTENSIONS,
  EXCEL_EXTENSIONS,
  HTML_EXTENSIONS,
  IMAGE_EXTENSIONS,
  TEXT_EXTENSIONS,
  VIDEO_EXTENSIONS,
  WORD_EXTENSIONS,
} from './file.extension';

/**
 * @description:
 * 根据后缀名判断文件是否是图片
 * 此处需要改进
 */
export const isImage = (fileName: string): boolean => {
  return isFileType(fileName, IMAGE_EXTENSIONS);
};

/**
 * @description: 判断是否为视频
 * @param {string} fileName
 * @return {boolean}
 */
export const isVideo = (fileName: string): boolean => {
  return isFileType(fileName, VIDEO_EXTENSIONS);
};

/**
 * @description: 判断是否为文档
 * @param {string} fileName
 * @return {boolean}
 */
export const isDoc = (fileName: string): boolean => {
  return isFileType(fileName, DOC_EXTENSIONS);
};

export const isTxt = (fileName: string) => {
  return isFileType(fileName, TEXT_EXTENSIONS);
};

export const isExcel = (fileName: string) => {
  return isFileType(fileName, EXCEL_EXTENSIONS);
};
/**
 * @description: 根据文件名判断是否为HTML
 * @param {string} fileName
 */
export const isHtml = (fileName: string) => {
  return isFileType(fileName, HTML_EXTENSIONS);
};

/**
 * @description: 判断是否为已知类型的文件
 * @param {string} fileName
 */
const isFileType = (fileName: string, fileTypes: string[]): boolean => {
  const extension = getFileExtension(fileName);
  if (!extension) return false;
  return fileTypes.includes(extension);
};

/**
 * @description: 获取文件类型
 * @param {string} fileName
 */
export const getFileType = (fileName?: string): FILE_ENUM => {
  if (!fileName) return FILE_ENUM.OTHER;
  const extension = getFileExtension(fileName);
  if (!extension) return FILE_ENUM.OTHER;
  if (
    [...DOC_EXTENSIONS, ...HTML_EXTENSIONS, ...EXCEL_EXTENSIONS, ...TEXT_EXTENSIONS].includes(
      fileName,
    )
  ) {
    return FILE_ENUM.DOC;
  } else if (isImage(fileName)) {
    return FILE_ENUM.IMAGE;
  } else if (isVideo(fileName)) {
    return FILE_ENUM.VIDEO;
  } else {
    return FILE_ENUM.OTHER;
  }
};
/**
 * @description: 判断是否WORD
 * @param {string} filename 文件名
 */
export const isWordDocument = (filename: string) => {
  const fileExtension = filename.slice(((filename.lastIndexOf('.') - 1) >>> 0) + 2);
  return WORD_EXTENSIONS.includes(`.${fileExtension}`);
};
